home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Behaviors / Actions / Timeline / Go To Timeline Frame.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  7.1 KB  |  187 lines

  1. //*************** GLOBALS VARS *****************
  2.  
  3. var helpDoc = MM.HELP_behGoToTimelineFrame;
  4.  
  5. //******************* BEHAVIOR FUNCTION **********************
  6.  
  7. //Jumps a timeline to a new frame.
  8. //Accepts the following arguments:
  9. //  tmLnName - the name of the timeline (ex: Timeline1)
  10. //  fNew     - the frame number to jump to
  11. //  numGotos - (optional) the number of times to jump there
  12. //
  13. //Designed to work in conjunction with Dreamweaver's Timeline Inspector.
  14. //The Timeline Inspector creates a JS function called MM_initTimelines(),
  15. //which puts all the timeline settings in a multidimensional array, saved
  16. //into a document property called document.MM_Time.
  17. //
  18. //My function initializes the timeline by calling MM_initTimelines.
  19. //Then it checks the data arrays and sets all sprites to their new positions
  20. //and sets other properties. For behavior sprites it evals the behavior call.
  21. //If numGotos is set, this function will disable itself. For example, if you call:
  22. //  MM_timelineGoto("Timeline1", "1", "3");
  23. //this function will work the first and second time, but not the third, so
  24. //that the timeline can continue after "3" loops.
  25.  
  26. function MM_timelineGoto(tmLnName, fNew, numGotos) { //v2.0
  27.   //Copyright 1997 Macromedia, Inc. All rights reserved.
  28.   var i,j,tmLn,props,keyFrm,sprite,numKeyFr,firstKeyFr,lastKeyFr,propNum,theObj;
  29.   if (document.MM_Time == null) MM_initTimelines(); //if *very* 1st time
  30.   tmLn = document.MM_Time[tmLnName];
  31.   if (numGotos != null)
  32.     if (tmLn.gotoCount == null) tmLn.gotoCount = 1;
  33.     else if (tmLn.gotoCount++ >= numGotos) {tmLn.gotoCount=0; return}
  34.   jmpFwd = (fNew > tmLn.curFrame);
  35.   for (i = 0; i < tmLn.length; i++) {
  36.     sprite = (jmpFwd)? tmLn[i] : tmLn[(tmLn.length-1)-i]; //count bkwds if jumping back
  37.     if (sprite.charAt(0) == "s") {
  38.       numKeyFr = sprite.keyFrames.length;
  39.       firstKeyFr = sprite.keyFrames[0];
  40.       lastKeyFr = sprite.keyFrames[numKeyFr - 1];
  41.       if ((jmpFwd && fNew<firstKeyFr) || (!jmpFwd && lastKeyFr<fNew)) continue; //skip if untouchd
  42.       for (keyFrm=1; keyFrm<numKeyFr && fNew>=sprite.keyFrames[keyFrm]; keyFrm++);
  43.       for (j=0; j<sprite.values.length; j++) {
  44.         props = sprite.values[j];
  45.         if (numKeyFr == props.length) propNum = keyFrm-1 //keyframes only
  46.         else propNum = Math.min(Math.max(0,fNew-firstKeyFr),props.length-1); //or keep in legal range
  47.         if (sprite.obj != null) {
  48.           if (props.prop2 == null) sprite.obj[props.prop] = props[propNum];
  49.           else        sprite.obj[props.prop2][props.prop] = props[propNum];
  50.       } }
  51.     } else if (sprite.charAt(0)=='b' && fNew == sprite.frame) eval(sprite.value);
  52.   }
  53.   tmLn.curFrame = fNew;
  54.   if (tmLn.ID == 0) eval('MM_timelinePlay(tmLnName)');
  55. }
  56.  
  57.  
  58. //******************* API **********************
  59.  
  60.  
  61. //Checks for the existence of timelines.
  62. //If none exist, returns false so this Action is grayed out.
  63.  
  64. function canAcceptBehavior(){
  65.   var allScripts,i,theScript,timelineExists;
  66.  
  67.   timelineExists = false;
  68.   allScripts = getObjectTags("document","script");
  69.   for (i in allScripts) {
  70.     theScript = ""+allScripts[i];
  71.     if (theScript.indexOf("function MM_initTimelines") != -1) {
  72.       timelineExists = true;
  73.       break;
  74.   } }
  75.   return (timelineExists);
  76. }
  77.  
  78.  
  79.  
  80. //Returns a Javascript function to be inserted in HTML head with script tags.
  81.  
  82. function behaviorFunction(){
  83.   return "MM_timelineGoto";
  84. }
  85.  
  86.  
  87.  
  88. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  89. //Returns two args: the selected timeline name, and the frmNum to goto.
  90.  
  91. function applyBehavior(x,y,theEvent) {
  92.   var menuIndex, timelineName, frmNum, numGotos, curFrm;
  93.   var eventFromTI = "onFrame";  //event prefix passed to applyBehavior from Timeline Inspector
  94.  
  95.   menuIndex = document.theForm.menu.selectedIndex;  //get menu selection index
  96.   timelineName = document.theForm.menu.options[menuIndex].text; //gets selected string
  97.   frmNum = parseInt(document.theForm.frmNum.value); //try and get frame num
  98.   if ((document.theForm.frmNum.value != ""+frmNum) ||  //if not a number
  99.       (frmNum < 1))                                    //or if < 1
  100.     return MSG_NegFrameNum;
  101.   else
  102.     if (document.theForm.numGotos.value) { //if not empty
  103.       numGotos = parseInt(document.theForm.numGotos.value); //try and get numGotos
  104.       if ((document.theForm.numGotos.value != ""+numGotos) || //if not a number
  105.           (numGotos < 1))                                     //or if < 1
  106.            return MSG_NegGotoNum;
  107.  
  108.       else { //user entered a Loop value
  109.         if (theEvent.indexOf(eventFromTI) == -1) { //if event is not from Timeline Inspector
  110.           document.theForm.numGotos.value == ""; //clear loop value
  111.           return MSG_LoopNotFromTI;
  112.         }
  113.         curFrm = parseInt(theEvent.substring(eventFromTI.length,theEvent.length)); //get frame #
  114.         if (frmNum > curFrm) { //if not jumping backward
  115.           document.theForm.numGotos.value == ""; //clear loop value
  116.           return MSG_LoopNotBackward;
  117.         } 
  118.         else { //everything's okay, allow loop value
  119.           return "MM_timelineGoto('"+timelineName+"','"+frmNum+"','"+(numGotos-1)+"')";
  120.         }
  121.       }
  122.     }
  123.     else return "MM_timelineGoto('"+timelineName+"','"+frmNum+"')";
  124. }
  125.  
  126.  
  127.  
  128. //Passed the function call above, extracts the args and reloads the UI.
  129. //With arg timelineName, scans the menu for a matching item, and selects it.
  130. //If the name is not found, it gives an error msg.
  131. //With arg frmNum, stores in text input.
  132.  
  133. function inspectBehavior(upStr){
  134.   var timelineName,menuLength,i;
  135.   var argArray = new Array;
  136.   var found = false;
  137.  
  138.   argArray = extractArgs(upStr); //get args
  139.   if (argArray.length > 2) {  //should be 3 or 4 args (first arg is fn call, ignored)
  140.     timelineName = argArray[1];
  141.     menuLength = document.theForm.menu.options.length;
  142.     for (var i=0; i<menuLength; i++) {  //search menu for matching timeline name
  143.       if (document.theForm.menu.options[i].text == timelineName) { //if found
  144.         document.theForm.menu.selectedIndex = i;  //make it selected
  145.         found = true;
  146.         break;
  147.       }
  148.     }
  149.     if (!found) alert(errMsg(MSG_TimelineNotFound,timelineName));
  150.     document.theForm.frmNum.value = argArray[2];
  151.     if (argArray.length > 3) document.theForm.numGotos.value = parseInt(argArray[3])+1;
  152.   }
  153. }
  154.  
  155.  
  156.  
  157. //***************** LOCAL FUNCTIONS  ******************
  158.  
  159.  
  160. //Load the select menu with timeline names.
  161.  
  162. function initializeUI(){
  163.   var i,j,startPos,endPos,theName,theScript;
  164.   var menuIndex = 0;
  165.   var allScripts = new Array;
  166.  
  167.   allScripts = getObjectTags("document","script");
  168.   for (i in allScripts) {
  169.     theScript = ""+allScripts[i];
  170.     if (theScript.indexOf("function MM_initTimelines") != -1) {
  171.       j = theScript.indexOf('].MM_Name');
  172.       while (j != -1) {
  173.         startPos = theScript.indexOf('"',++j);
  174.         endPos = theScript.indexOf('"',++startPos);
  175.         if (0 < startPos && startPos < endPos) {
  176.           theName = theScript.substring(startPos,endPos);
  177.           document.theForm.menu.options[menuIndex++] = new Option(theName);
  178.         }
  179.         j = theScript.indexOf('].MM_Name',j+1);
  180.       }
  181.     }
  182.   }
  183.  
  184.   document.theForm.frmNum.focus(); //set focus on textbox
  185.   document.theForm.frmNum.select(); //set insertion point into textbox
  186. }
  187.